﻿using System;
using System.Data;
using System.Drawing;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI;
//using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;

using DevExpress.Web.ASPxGridView;
using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Web;

using LMD.Lib.DataClass;
using LMD.Constant;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;
using LMD.Lib.Controls;

/// <summary>
/// Summary description for CompanyReportManager
/// </summary>
public static class CompanyReportManager
{
    public static String GetReportName(int iReportId)
    {
        String sxReturn = String.Empty;

        switch (iReportId)
        {
            case 1: return "Report Billing & Pembayaran";
            case 2: return "Report Jaringan Order";
            case 3: return "Report Gangguan";
            case 4: return "Report Waiting List";
            case 5: return "Report Opportunity";
            case 6: return "Report Saldo Akhir";
            case 7: return "Report Jasa & Jaringan";
            case 8: return "Report Penyebab Gangguan";
            case 9: return "Report Aktivitas CAR Sales";
        }

        return sxReturn;
    }
    public static void GenerateGrid(ASPxGridView dgMain, int iReportId, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {

        switch (iReportId)
        {
            case 1: GenerateGrid_RevenuePembayaran(dgMain, iCompanyId, dtmStart, dtmEnd); break;
            case 2: GenerateGrid_NetworkJasaTable(dgMain, iCompanyId, dtmStart, dtmEnd); break;
            case 3: GenerateGrid_GangguanTable(dgMain, iCompanyId, dtmStart, dtmEnd); break;
            case 4: GenerateGrid_WaitingListTable(dgMain, iCompanyId, dtmStart, dtmEnd); break;
            case 5: GenerateGrid_OpportunityTable(dgMain, iCompanyId, dtmStart, dtmEnd); break;
            case 6: GenerateGrid_SaldoAkhirTable(dgMain, iCompanyId, dtmStart, dtmEnd); break;
            case 7: GenerateGrid_JasaJaringanTable(dgMain, iCompanyId, dtmStart, dtmEnd); break;
            case 8: GenerateGrid_PenyebabGangguanTable(dgMain, iCompanyId, dtmStart, dtmEnd); break;
            case 9: GenerateGrid_AktivitasCARSalesTable(dgMain, iCompanyId, dtmStart, dtmEnd); break;
        }
    }
    public static void GenerateChart(WebChartControl chrMain, int iReportId, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {

        switch (iReportId)
        {
            case 1: GenerateChart_RevenuePembayaran(chrMain, iCompanyId, dtmStart, dtmEnd, sxFilteredSeries); break;
            case 2: GenerateChart_NetworkJasaTable(chrMain, iCompanyId, dtmStart, dtmEnd, sxFilteredSeries); break;
            case 3: GenerateChart_GangguanTable(chrMain, iCompanyId, dtmStart, dtmEnd, sxFilteredSeries); break;
            case 4: GenerateChart_WaitingListTable(chrMain, iCompanyId, dtmStart, dtmEnd, sxFilteredSeries); break;
            case 5: GenerateChart_OpportunityTable(chrMain, iCompanyId, dtmStart, dtmEnd, sxFilteredSeries); break;
            case 6: GenerateChart_SaldoAkhirTable(chrMain, iCompanyId, dtmStart, dtmEnd, sxFilteredSeries); break;
            case 7: GenerateChart_JasaJaringanTable(chrMain, iCompanyId, dtmStart, dtmEnd, sxFilteredSeries); break;
            case 8: GenerateChart_PenyebabGangguanTable(chrMain, iCompanyId, dtmStart, dtmEnd, sxFilteredSeries); break;
            case 9: GenerateChart_AktivitasCARSalesTable(chrMain, iCompanyId, dtmStart, dtmEnd, sxFilteredSeries); break;
        }
    }

    private static void GenerateGrid_RevenuePembayaran(ASPxGridView dgMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        Cached.CompanyReport_Pembayaran ccTmp = new Cached.CompanyReport_Pembayaran();
        DataTable dt = ccTmp.GetPembayaranDetail(iCompanyId, dtmStart, dtmEnd);
        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        dgMain.Caption = String.Format("{0}: {1} to {2}", GetReportName(1), sxStartDate, sxEndDate);
        dgMain.DataSource = dt;
        dgMain.DataBind();

        dgMain.Settings.ShowHorizontalScrollBar = true;

        dgMain.Columns["ACURRENCY"].Caption = "Mata Uang";
        dgMain.Columns["PERIODE"].Caption = "Periode";
        dgMain.Columns["SALDO"].Caption = "Saldo";
        dgMain.Columns["DEBET"].Caption = "Debet";
        dgMain.Columns["KOREKSI"].Caption = "Koreksi";
        dgMain.Columns["KREDIT"].Caption = "Kredit";
        dgMain.Columns["PERCENT_K"].Caption = "% Kredit";
        dgMain.Columns["KREDIT_30"].Caption = "Pelunasan (H1-H30)";
        dgMain.Columns["PERCENT_K30"].Caption = "";
        dgMain.Columns["KREDIT_31_45"].Caption = "Pelunasan (H1-H30)";
        dgMain.Columns["PERCENT_K31"].Caption = "";
        dgMain.Columns["KREDIT_46_60"].Caption = "Pelunasan (H46-H60)";
        dgMain.Columns["PERCENT_K46"].Caption = "";
        dgMain.Columns["KREDIT_61_90"].Caption = "Pelunasan (H61-H90)";
        dgMain.Columns["PERCENT_K61"].Caption = "";
        dgMain.Columns["KREDIT_91"].Caption = "Pelunasan (H91++)";
        dgMain.Columns["PERCENT_K91"].Caption = "";

        foreach (DataColumn dc in ((DataTable)dgMain.DataSource).Columns)
        {
            if (dc.DataType == typeof(Decimal))
            {
                ((GridViewDataTextColumn)dgMain.Columns[dc.ColumnName]).PropertiesTextEdit.DisplayFormatString = "n";
            }
        }
    }
    private static void GenerateChart_RevenuePembayaran(WebChartControl chrMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {
        Cached.CompanyReport_Pembayaran ccTmp = new Cached.CompanyReport_Pembayaran();
        DataTable dt = ccTmp.GetPembayaranRekap(iCompanyId, dtmStart, dtmEnd);

        //Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        //CompanyProfileData cpd = ccCompany.GetCompanyProfileData(iCompanyId);

        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        chrMain.Titles.Clear();
        ChartTitle _Title = new ChartTitle();
        _Title.Text = String.Format("{0}: {1} to {2}", GetReportName(1), sxStartDate, sxEndDate);
        _Title.Dock = ChartTitleDockStyle.Top;
        _Title.Font = new Font("Arial", 14);
        chrMain.Titles.Add(_Title);

        Series[] arrSeries;
        Color[] arrColors;
        String[] sxSeries = sxFilteredSeries.Split(new string[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        if (!String.IsNullOrEmpty(sxFilteredSeries))
        {
            arrSeries = new Series[sxSeries.Length];
            for (int idx = 0; idx < sxSeries.Length; idx++)
            {
                if (!String.IsNullOrEmpty(sxSeries[idx]))
                {
                    arrSeries[idx] = new Series(sxSeries[idx], ViewType.Bar);
                }
            }
        }
        else
        {
            arrSeries = new Series[2];
            arrSeries[0] = new Series("DEBET", ViewType.Bar);
            arrSeries[1] = new Series("KOREKSI", ViewType.Bar);
        }
        arrColors = new Color[arrSeries.Length];
        for (int i = 0; i < arrSeries.Length; i++)
        {
            arrColors[i] = Color.FromArgb(i, Color.Blue);
        }

        int iEnd = dt.Rows.Count;
        int iStart = iEnd <= 12 ? 0 : iEnd - 12;

        DateTime dtmTmp = dtmEnd.AddMonths(-11);

        dtmTmp = dtmTmp.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;

        while (dtmTmp.CompareTo(dtmEnd) <= 0)
        {
            DataRow[] dr = dt.Select(
                    String.Format("MONTH = {0} and YEAR = {1}", dtmTmp.Month, dtmTmp.Year));

            for (int i = 0; i < arrSeries.Length; i++)
            {
                Decimal decValue = dr.Length == 0 ? 0 : Convert.ToDecimal(dr[0][arrSeries[i].Name]);
                arrSeries[i].Points.Add(new SeriesPoint(dtmTmp.ToString("MMM/yy"), Math.Round(decValue/1000000,2)));
            }

            dtmTmp = dtmTmp.AddMonths(1);
        }

        chrMain.Series.Clear();
        for (int x = 0; x < arrSeries.Length; x++)
        {
            arrSeries[x].PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].Label.Visible = true;
            arrSeries[x].Label.BackColor = System.Drawing.Color.Transparent;
            arrSeries[x].Label.Border.Color = System.Drawing.Color.Transparent;
            //((BarSeriesView)arrSeries[x].View).Color = arrColors[x];
            chrMain.Series.Add(arrSeries[x]);
        }

        ((XYDiagram)chrMain.Diagram).AxisX.Label.Staggered = true;
        ((XYDiagram)chrMain.Diagram).AxisY.Title.Text = "(dalam satuan juta Rupiah)";
        chrMain.DataBind();
    }

    private static void GenerateGrid_NetworkJasaTable(ASPxGridView dgMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        Cached.CompanyReport_NetworkJasaTable ccTmp = new Cached.CompanyReport_NetworkJasaTable();
        DataTable dt = ccTmp.GetNetworkJasaDetail(iCompanyId, dtmStart, dtmEnd);
        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        dgMain.Caption = String.Format("{0}: {1} to {2}", GetReportName(2), sxStartDate, sxEndDate);

        dgMain.DataSource = dt;
        dgMain.DataBind();

        dgMain.Settings.ShowHorizontalScrollBar = false;

        dgMain.Columns["NETWORK_NO"].Caption = "Nomor Jaringan";
        dgMain.Columns["STATUS_JAR"].Caption = "Status";
        dgMain.Columns["JASA1"].Caption = "Jasa 1";
        dgMain.Columns["JASA2"].Caption = "Jasa 2";
        dgMain.Columns["JASA3"].Caption = "Jasa 3";
    }
    private static void GenerateChart_NetworkJasaTable(WebChartControl chrMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {
        Cached.CompanyReport_NetworkJasaTable ccTmp = new Cached.CompanyReport_NetworkJasaTable();
        DataTable dt = ccTmp.GetNetworkJasaRekap(iCompanyId, dtmStart, dtmEnd);

        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        CompanyProfileData cpd = ccCompany.GetCompanyProfileData(iCompanyId);

        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        chrMain.Titles.Clear();
        ChartTitle _Title = new ChartTitle();
        _Title.Text = String.Format("{0}: {1} to {2}", GetReportName(2), sxStartDate, sxEndDate);
        _Title.Dock = ChartTitleDockStyle.Top;
        _Title.Font = new Font("Arial", 14);
        chrMain.Titles.Add(_Title);

        Series[] arrSeries;
        Color[] arrColors;
        String[] sxSeries = sxFilteredSeries.Split(new string[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        if (!String.IsNullOrEmpty(sxFilteredSeries))
        {
            arrSeries = new Series[sxSeries.Length];
            for (int idx = 0; idx < sxSeries.Length; idx++)
            {
                if (!String.IsNullOrEmpty(sxSeries[idx]))
                {
                    arrSeries[idx] = new Series(sxSeries[idx], ViewType.Line);
                }
            }
        }
        else
        {
            arrSeries = new Series[5];
            arrSeries[0] = new Series("ONLINE BILLING", ViewType.Line);
            arrSeries[1] = new Series("ISOLIR", ViewType.Line);
            arrSeries[2] = new Series("CABUT", ViewType.Line);
            arrSeries[3] = new Series("PROSES ISOLIR", ViewType.Line);
            arrSeries[4] = new Series("PROSES CABUT", ViewType.Line);
        }
        arrColors = new Color[arrSeries.Length];
        for (int i = 0; i < arrSeries.Length; i++)
        {
            arrColors[i] = Color.FromArgb(i, Color.Blue);
        }

        //Series[] arrSeries = new Series[5];
        //arrSeries[0] = new Series("Online Billing", ViewType.Line);
        //arrSeries[1] = new Series("Isolir", ViewType.Line);
        //arrSeries[2] = new Series("Cabut", ViewType.Line);
        //arrSeries[3] = new Series("Proses Isolir", ViewType.Line);
        //arrSeries[4] = new Series("Proses Cabut", ViewType.Line);

        //Color[] arrColors = new Color[5];
        //arrColors[0] = Color.SkyBlue;
        //arrColors[1] = Color.Orange;
        //arrColors[2] = Color.Red;
        //arrColors[3] = Color.PeachPuff;
        //arrColors[4] = Color.Pink;

        int iEnd = dt.Rows.Count;
        int iStart = iEnd <= 12 ? 0 : iEnd - 12;

        DateTime dtmTmp = dtmEnd.AddMonths(-11);

        dtmTmp = dtmTmp.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;

        while (dtmTmp.CompareTo(dtmEnd) <= 0)
        {
            DataRow[] dr = dt.Select(
                    String.Format("MONTH = {0} and YEAR = {1}", dtmTmp.Month, dtmTmp.Year));

            for (int i = 0; i < arrSeries.Length; i++)
            {
                int iValue = dr.Length == 0 ? 0 : Convert.ToInt32(dr[0][arrSeries[i].Name]);
                arrSeries[i].Points.Add(new SeriesPoint(dtmTmp.ToString("MMM/yy"), iValue));
            }

            dtmTmp = dtmTmp.AddMonths(1);
        }

        //for (int i = iStart; i < iEnd; i++)
        //{
        //    for (int j = 0; j < arrSeries.Length; j++)
        //    {
        //        int iValue = dt.Rows[i][j] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][j]);
        //        arrSeries[j].Points.Add(new SeriesPoint(dt.Rows[i]["VALUE"].ToString(), iValue));
        //    }

        //}
        //_srs1.Points.Add(new SeriesPoint("Online Billing", Convert.ToInt32(obj[0])));
        //_srs1.Points.Add(new SeriesPoint("Isolir", Convert.ToInt32(obj[1])));
        //_srs1.Points.Add(new SeriesPoint("Cabut", Convert.ToInt32(obj[2])));
        //_srs1.Points.Add(new SeriesPoint("Proses Isolir", Convert.ToInt32(obj[3])));
        //_srs1.Points.Add(new SeriesPoint("Proses Cabut", Convert.ToInt32(obj[4])));
        chrMain.Series.Clear();
        for (int x = 0; x < arrSeries.Length; x++)
        {
            arrSeries[x].PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].Label.Visible = true;
            arrSeries[x].Label.BackColor = System.Drawing.Color.Transparent;
            arrSeries[x].Label.Border.Color = System.Drawing.Color.Transparent;
            ((LineSeriesView)arrSeries[x].View).Color = arrColors[x];
            //((LineSeriesView)arrSeries[x].View).Transparency = byte.Parse("120");
            chrMain.Series.Add(arrSeries[x]);
        }
        //_srs1.PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
        //_srs1.LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
        //_srs1.Label.Visible = true;

        ((XYDiagram)chrMain.Diagram).AxisX.Label.Staggered = true;
        ((XYDiagram)chrMain.Diagram).AxisY.Title.Text = cpd.sxCompanyName;
        chrMain.DataBind();

    }

    private static void GenerateGrid_GangguanTable(ASPxGridView dgMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        Cached.CompanyReport_GangguanTable ccTmp = new Cached.CompanyReport_GangguanTable();
        DataTable dt = ccTmp.GetGangguanDetail(iCompanyId, dtmStart, dtmEnd);
        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        dgMain.Caption = String.Format("{0}: {1} to {2}", GetReportName(3), sxStartDate, sxEndDate);

        dgMain.DataSource = dt;
        dgMain.DataBind();

        dgMain.Settings.ShowHorizontalScrollBar = false;

        dgMain.Columns["NETWORK_NO"].Caption = "Nomor Jaringan";
        dgMain.Columns["TROUBLE_START_DATE_TIME"].Caption = "Waktu Gangguan";
        dgMain.Columns["REPEATED"].Caption = "Gangguan Berulang";
    }
    private static void GenerateChart_GangguanTable(WebChartControl chrMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {
        Cached.CompanyReport_GangguanTable ccTmp = new Cached.CompanyReport_GangguanTable();
        DataTable dt = ccTmp.GetGangguanRekap(iCompanyId, dtmStart, dtmEnd);

        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        CompanyProfileData cpd = ccCompany.GetCompanyProfileData(iCompanyId);

        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        chrMain.Titles.Clear();
        ChartTitle _Title = new ChartTitle();
        _Title.Text = String.Format("{0}: {1} to {2}", GetReportName(3), sxStartDate, sxEndDate);
        _Title.Dock = ChartTitleDockStyle.Top;
        _Title.Font = new Font("Arial", 14);
        chrMain.Titles.Add(_Title);

        Series[] arrSeries;
        Color[] arrColors;
        String[] sxSeries = sxFilteredSeries.Split(new string[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        if (!String.IsNullOrEmpty(sxFilteredSeries))
        {
            int iLength = 0;
            for (int i = 0; i < sxSeries.Length; i++)
            {
                if (!String.IsNullOrEmpty(sxSeries[i]))
                {
                    iLength++;
                }
            }
            arrSeries = new Series[iLength];
            int iLength2 = 0;
            for (int idx = 0; idx < sxSeries.Length; idx++)
            {
                if (!String.IsNullOrEmpty(sxSeries[idx]))
                {
                    arrSeries[iLength2] = new Series(sxSeries[idx], ViewType.Bar);
                    iLength2++;
                }
            }
        }
        else
        {
            arrSeries = new Series[2];
            arrSeries[0] = new Series("GANGGUAN BERULANG", ViewType.Bar);
            arrSeries[1] = new Series("BUKAN GANGGUAN BERULANG", ViewType.Bar);
        }
        arrColors = new Color[arrSeries.Length];
        for (int i = 0; i < arrSeries.Length; i++)
        {
            arrColors[i] = Color.FromArgb(i, Color.Blue);
        }

        //Series[] arrSeries = new Series[2];
        //arrSeries[0] = new Series("Gangguan Berulang", ViewType.Line);
        //arrSeries[1] = new Series("Bukan Gangguan Berulang", ViewType.Line);

        //Color[] arrColors = new Color[2];
        //arrColors[0] = Color.SkyBlue;
        //arrColors[1] = Color.Orange;

        int iEnd = dt.Rows.Count;
        int iStart = iEnd <= 12 ? 0 : iEnd - 12;

        DateTime dtmTmp = dtmEnd.AddMonths(-11);

        dtmTmp = dtmTmp.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;

        while (dtmTmp.CompareTo(dtmEnd) <= 0)
        {
            DataRow[] dr = dt.Select(
                    String.Format("MONTH = {0} and YEAR = {1}", dtmTmp.Month, dtmTmp.Year));

            for (int i = 0; i < arrSeries.Length; i++)
            {
                int iValue = dr.Length == 0 ? 0 : Convert.ToInt32(dr[0][arrSeries[i].Name]);
                arrSeries[i].Points.Add(new SeriesPoint(dtmTmp.ToString("MMM/yy"), iValue));
            }

            dtmTmp = dtmTmp.AddMonths(1);
        }
        chrMain.Series.Clear();
        for (int x = 0; x < arrSeries.Length; x++)
        {
            arrSeries[x].PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].Label.Visible = true;
            arrSeries[x].Label.BackColor = System.Drawing.Color.Transparent;
            arrSeries[x].Label.Border.Color = System.Drawing.Color.Transparent;
            //((LineSeriesView)arrSeries[x].View).Color = arrColors[x];
            chrMain.Series.Add(arrSeries[x]);
        }

        ((XYDiagram)chrMain.Diagram).AxisX.Label.Staggered = true;
        ((XYDiagram)chrMain.Diagram).AxisY.Title.Text = cpd.sxCompanyName;
        chrMain.DataBind();

    }

    private static void GenerateGrid_WaitingListTable(ASPxGridView dgMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        Cached.CompanyReport_WaitingListTable ccTmp = new Cached.CompanyReport_WaitingListTable();
        DataTable dt = ccTmp.GetWaitingListDetail(iCompanyId, dtmStart, dtmEnd);
        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        dgMain.Caption = String.Format("{0}: {1} to {2}", GetReportName(4), sxStartDate, sxEndDate);

        dgMain.DataSource = dt;
        dgMain.DataBind();

        dgMain.Settings.ShowHorizontalScrollBar = false;

        dgMain.Columns["NETWORK_NO"].Caption = "Nomor Jaringan";
        dgMain.Columns["JENIS_ORDER"].Caption = "Jenis Order";
        dgMain.Columns["TGL_ORDER"].Caption = "Tanggal";
        dgMain.Columns["MASALAH"].Caption = "Masalah";
        dgMain.Columns["STOP_CLOCK"].Caption = "Stop Clock";
        dgMain.Columns["LAMA_WAITING_LIST"].Caption = "Lama";
    }
    private static void GenerateChart_WaitingListTable(WebChartControl chrMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {
        Cached.CompanyReport_WaitingListTable ccTmp = new Cached.CompanyReport_WaitingListTable();
        DataTable dt = ccTmp.GetWaitingListRekap(iCompanyId, dtmStart, dtmEnd);

        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        CompanyProfileData cpd = ccCompany.GetCompanyProfileData(iCompanyId);

        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        chrMain.Titles.Clear();
        ChartTitle _Title = new ChartTitle();
        _Title.Text = String.Format("{0}: {1} to {2}", GetReportName(4), sxStartDate, sxEndDate);
        _Title.Dock = ChartTitleDockStyle.Top;
        _Title.Font = new Font("Arial", 14);
        chrMain.Titles.Add(_Title);

        Series[] arrSeries;
        Color[] arrColors;
        String[] sxSeries = sxFilteredSeries.Split(new string[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        if (!String.IsNullOrEmpty(sxFilteredSeries))
        {
            arrSeries = new Series[sxSeries.Length];
            for (int idx = 0; idx < sxSeries.Length; idx++)
            {
                if (!String.IsNullOrEmpty(sxSeries[idx]))
                {
                    arrSeries[idx] = new Series(sxSeries[idx], ViewType.Line);
                }
            }
        }
        else
        {
            arrSeries = new Series[7];
            arrSeries[0] = new Series("PASANG BARU", ViewType.Line);
            arrSeries[1] = new Series("MUTASI", ViewType.Line);
            arrSeries[2] = new Series("CABUT", ViewType.Line);
            arrSeries[3] = new Series("ISOLIR", ViewType.Line);
            arrSeries[4] = new Series("BATAL PASANG BARU", ViewType.Line);
            arrSeries[5] = new Series("BATAL MUTASI", ViewType.Line);
            arrSeries[6] = new Series("BUKA ISOLIR", ViewType.Line);
        }
        arrColors = new Color[arrSeries.Length];
        for (int i = 0; i < arrSeries.Length; i++)
        {
            arrColors[i] = Color.FromArgb(i, Color.Blue);
        }

        //Series[] arrSeries = new Series[7];
        //arrSeries[0] = new Series("Pasang Baru", ViewType.Line);
        //arrSeries[1] = new Series("Mutasi", ViewType.Line);
        //arrSeries[2] = new Series("Cabut", ViewType.Line);
        //arrSeries[3] = new Series("Isolir", ViewType.Line);
        //arrSeries[4] = new Series("Batal Pasang Baru", ViewType.Line);
        //arrSeries[5] = new Series("Batal Mutasi", ViewType.Line);
        //arrSeries[6] = new Series("Buka Isolir", ViewType.Line);

        //Color[] arrColors = new Color[7];
        //arrColors[0] = Color.SkyBlue;
        //arrColors[1] = Color.Orange;
        //arrColors[2] = Color.Red;
        //arrColors[3] = Color.PeachPuff;
        //arrColors[4] = Color.Pink;
        //arrColors[5] = Color.Purple;
        //arrColors[6] = Color.Blue;

        int iEnd = dt.Rows.Count;
        int iStart = iEnd <= 12 ? 0 : iEnd - 12;

        DateTime dtmTmp = dtmEnd.AddMonths(-11);

        dtmTmp = dtmTmp.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;

        while (dtmTmp.CompareTo(dtmEnd) <= 0)
        {
            DataRow[] dr = dt.Select(
                    String.Format("MONTH = {0} and YEAR = {1}", dtmTmp.Month, dtmTmp.Year));

            for (int i = 0; i < arrSeries.Length; i++)
            {
                int iValue = dr.Length == 0 ? 0 : Convert.ToInt32(dr[0][arrSeries[i].Name]);
                arrSeries[i].Points.Add(new SeriesPoint(dtmTmp.ToString("MMM/yy"), iValue));
            }

            dtmTmp = dtmTmp.AddMonths(1);
        }

        chrMain.Series.Clear();
        for (int x = 0; x < arrSeries.Length; x++)
        {
            arrSeries[x].PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].Label.Visible = true;
            arrSeries[x].Label.BackColor = System.Drawing.Color.Transparent;
            arrSeries[x].Label.Border.Color = System.Drawing.Color.Transparent;
            ((LineSeriesView)arrSeries[x].View).Color = arrColors[x];
            chrMain.Series.Add(arrSeries[x]);
        }

        ((XYDiagram)chrMain.Diagram).AxisX.Label.Staggered = true;
        ((XYDiagram)chrMain.Diagram).AxisY.Title.Text = cpd.sxCompanyName;
        chrMain.DataBind();

    }

    private static void GenerateGrid_OpportunityTable(ASPxGridView dgMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        Cached.CompanyReport_OpportunityTable ccTmp = new Cached.CompanyReport_OpportunityTable();
        DataTable dt = ccTmp.GetOpportunityDetail(iCompanyId, dtmStart, dtmEnd);
        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        dgMain.Caption = String.Format("{0}: {1} to {2}", GetReportName(5), sxStartDate, sxEndDate);

        dgMain.DataSource = dt;
        dgMain.DataBind();

        dgMain.Settings.ShowHorizontalScrollBar = false;

        dgMain.Columns["OPPORTUNITY_NO"].Caption = "Nomor Opportunity";
        dgMain.Columns["STATUS"].Caption = "Status";
        dgMain.Columns["START_DATE"].Caption = "Tanggal Mulai";
        dgMain.Columns["END_DATE"].Caption = "Tanggal Selesai";
        dgMain.Columns["STAGE_NAME"].Caption = "Stage";
    }
    private static void GenerateChart_OpportunityTable(WebChartControl chrMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {
        Cached.CompanyReport_OpportunityTable ccTmp = new Cached.CompanyReport_OpportunityTable();
        DataTable dt = ccTmp.GetOpportunityRekap(iCompanyId, dtmStart, dtmEnd);

        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        CompanyProfileData cpd = ccCompany.GetCompanyProfileData(iCompanyId);

        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        chrMain.Titles.Clear();
        ChartTitle _Title = new ChartTitle();
        _Title.Text = String.Format("{0}: {1} to {2}", GetReportName(5), sxStartDate, sxEndDate);
        _Title.Dock = ChartTitleDockStyle.Top;
        _Title.Font = new Font("Arial", 14);
        chrMain.Titles.Add(_Title);

        Series[] arrSeries;
        Color[] arrColors;
        String[] sxSeries = sxFilteredSeries.Split(new string[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        if (!String.IsNullOrEmpty(sxFilteredSeries))
        {
            arrSeries = new Series[sxSeries.Length];
            for (int idx = 0; idx < sxSeries.Length; idx++)
            {
                if (!String.IsNullOrEmpty(sxSeries[idx]))
                {
                    arrSeries[idx] = new Series(sxSeries[idx], ViewType.Line);
                }
            }
        }
        else
        {
            arrSeries = new Series[5];
            arrSeries[0] = new Series("CLOSED - CANCEL", ViewType.Bar);
            arrSeries[1] = new Series("CLOSED - LOST", ViewType.Bar);
            arrSeries[2] = new Series("CLOSED - WON", ViewType.Bar);
            arrSeries[3] = new Series("INACTIVE", ViewType.Bar);
            arrSeries[4] = new Series("OPEN", ViewType.Bar);
        }
        arrColors = new Color[arrSeries.Length];
        for (int i = 0; i < arrSeries.Length; i++)
        {
            arrColors[i] = Color.FromArgb(i, Color.Blue);
        }

        //Series[] arrSeries = new Series[5];
        //arrSeries[0] = new Series("Closed - Cancel", ViewType.Bar);
        //arrSeries[1] = new Series("Closed - Lost", ViewType.Bar);
        //arrSeries[2] = new Series("Closed - Won", ViewType.Bar);
        //arrSeries[3] = new Series("Inactive", ViewType.Bar);
        //arrSeries[4] = new Series("Open", ViewType.Bar);

        //Color[] arrColors = new Color[5];
        //arrColors[0] = Color.SkyBlue;
        //arrColors[1] = Color.Orange;
        //arrColors[2] = Color.Red;
        //arrColors[3] = Color.PeachPuff;
        //arrColors[4] = Color.Pink;

        int iEnd = dt.Rows.Count;
        int iStart = iEnd <= 12 ? 0 : iEnd - 12;

        DateTime dtmTmp = dtmEnd.AddMonths(-11);

        dtmTmp = dtmTmp.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;

        while (dtmTmp.CompareTo(dtmEnd) <= 0)
        {
            DataRow[] dr = dt.Select(
                    String.Format("MONTH = {0} and YEAR = {1}", dtmTmp.Month, dtmTmp.Year));

            for (int i = 0; i < arrSeries.Length; i++)
            {
                int iValue = dr.Length == 0 ? 0 : Convert.ToInt32(dr[0][arrSeries[i].Name]);
                arrSeries[i].Points.Add(new SeriesPoint(dtmTmp.ToString("MMM/yy"), iValue));
            }

            dtmTmp = dtmTmp.AddMonths(1);
        }

        chrMain.Series.Clear();
        for (int x = 0; x < arrSeries.Length; x++)
        {
            arrSeries[x].PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].Label.Visible = true;
            arrSeries[x].Label.BackColor = System.Drawing.Color.Transparent;
            arrSeries[x].Label.Border.Color = System.Drawing.Color.Transparent;
            //((LineSeriesView)arrSeries[x].View).Color = arrColors[x];
            chrMain.Series.Add(arrSeries[x]);
        }

        ((XYDiagram)chrMain.Diagram).AxisX.Label.Staggered = true;
        ((XYDiagram)chrMain.Diagram).AxisY.Title.Text = cpd.sxCompanyName;
        chrMain.DataBind();

    }

    private static void GenerateGrid_SaldoAkhirTable(ASPxGridView dgMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        Cached.CompanyReport_SaldoAkhirTable ccTmp = new Cached.CompanyReport_SaldoAkhirTable();
        DataTable dt = ccTmp.GetSaldoAkhirDetail(iCompanyId, dtmStart, dtmEnd);
        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        dgMain.Caption = String.Format("{0}: {1} to {2}", GetReportName(6), sxStartDate, sxEndDate);
        dgMain.DataSource = dt;
        dgMain.DataBind();

        dgMain.Settings.ShowHorizontalScrollBar = true;

        dgMain.Columns["COMPANY_NO"].Caption = "Company No";
        dgMain.Columns["COMPANY_NAME"].Caption = "Nama Perusahaan";
        dgMain.Columns["MONTH_NAME"].Caption = "Bulan";
        dgMain.Columns["YEAR_NUM"].Caption = "Tahun";
        dgMain.Columns["JUMLAH_PENDAPATAN"].Caption = "Jumlah Pendapatan";
        dgMain.Columns["JUMLAH_JARINGAN"].Caption = "Jaringan OB";
        dgMain.Columns["MONTH_NUM"].Visible = false;

        ((GridViewDataTextColumn)dgMain.Columns["JUMLAH_PENDAPATAN"]).PropertiesTextEdit.DisplayFormatString = "n";

    }
    private static void GenerateChart_SaldoAkhirTable(WebChartControl chrMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {
        Cached.CompanyReport_SaldoAkhirTable ccTmp = new Cached.CompanyReport_SaldoAkhirTable();
        DataTable dt = ccTmp.GetSaldoAkhirRekap(iCompanyId, dtmStart, dtmEnd);

        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        CompanyProfileData cpd = ccCompany.GetCompanyProfileData(iCompanyId);

        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        chrMain.Titles.Clear();
        ChartTitle _Title = new ChartTitle();
        _Title.Text = String.Format("{0}: {1} to {2}", GetReportName(6), sxStartDate, sxEndDate);
        _Title.Dock = ChartTitleDockStyle.Top;
        _Title.Font = new Font("Arial", 14);
        chrMain.Titles.Add(_Title);

        Series[] arrSeries;
        Color[] arrColors;
        String[] sxSeries = sxFilteredSeries.Split(new string[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        if (!String.IsNullOrEmpty(sxFilteredSeries) && dt.Rows.Count > 1)
        {
            arrSeries = new Series[sxSeries.Length];
            for (int idx = 0; idx < sxSeries.Length; idx++)
            {
                if (!String.IsNullOrEmpty(sxSeries[idx]))
                {
                    arrSeries[idx] = new Series(sxSeries[idx], ViewType.Line);
                }
            }
        }
        else
        {
            arrSeries = new Series[1];
            arrSeries[0] = new Series("JUMLAH_PENDAPATAN", ViewType.Bar);
            //arrSeries[1] = new Series("Koreksi", ViewType.Bar);
        }
        arrColors = new Color[arrSeries.Length];
        for (int i = 0; i < arrSeries.Length; i++)
        {
            arrColors[i] = Color.FromArgb(i, Color.Blue);
        }

        //Series[] arrSeries = new Series[1];
        //arrSeries[0] = new Series("Jumlah Pendapatan", ViewType.Bar);
        ////arrSeries[1] = new Series("Koreksi", ViewType.Bar);

        //Color[] arrColors = new Color[1];
        //arrColors[0] = Color.Green;
        ////arrColors[1] = Color.OrangeRed;

        int iEnd = dt.Rows.Count;
        int iStart = iEnd <= 12 ? 0 : iEnd - 12;

        DateTime dtmTmp = dtmEnd.AddMonths(-11);

        dtmTmp = dtmTmp.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;

        while (dtmTmp.CompareTo(dtmEnd) <= 0)
        {
            DataRow[] dr = dt.Select(
                    String.Format("MONTH_NUM = {0} and YEAR_NUM = {1}", dtmTmp.Month, dtmTmp.Year));

            for (int i = 0; i < arrSeries.Length; i++)
            {
                Decimal decValue = dr.Length == 0 ? 0 : Convert.ToDecimal(dr[0][arrSeries[i].Name]);
                arrSeries[i].Points.Add(new SeriesPoint(dtmTmp.ToString("MMM/yy"), Math.Round(decValue / 1000000, 2)));
            }

            dtmTmp = dtmTmp.AddMonths(1);
        }

        chrMain.Series.Clear();
        for (int x = 0; x < arrSeries.Length; x++)
        {
            arrSeries[x].PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].Label.Visible = true;
            arrSeries[x].Label.BackColor = System.Drawing.Color.Transparent;
            arrSeries[x].Label.Border.Color = System.Drawing.Color.Transparent;
            //((BarSeriesView)arrSeries[x].View).Color = arrColors[x];
            chrMain.Series.Add(arrSeries[x]);
        }

        ((XYDiagram)chrMain.Diagram).AxisX.Label.Staggered = true;
        ((XYDiagram)chrMain.Diagram).AxisY.Title.Text = "(dalam satuan juta Rupiah)";
        chrMain.DataBind();
    }

    private static void GenerateGrid_JasaJaringanTable(ASPxGridView dgMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        Cached.CompanyReport_JasaJaringanTable ccTmp = new Cached.CompanyReport_JasaJaringanTable();
        DataTable dt = ccTmp.GetJasaJaringanDetail(iCompanyId, dtmStart, dtmEnd);
        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        dgMain.Caption = String.Format("{0}: {1} to {2}", GetReportName(7), sxStartDate, sxEndDate);
        dgMain.DataSource = dt;
        dgMain.DataBind();

        dgMain.Settings.ShowHorizontalScrollBar = true;

        dgMain.Columns["COMPANY_NO"].Caption = "Company No";
        dgMain.Columns["COMPANY_NAME"].Caption = "Nama Perusahaan";
        dgMain.Columns["MONTH_NAME"].Caption = "Bulan";
        dgMain.Columns["YEAR_NUM"].Caption = "Tahun";
        dgMain.Columns["JUMLAH_JARINGAN"].Caption = "Jumlah Jaringan";
        dgMain.Columns["JASA_LEVEL_1"].Caption = "Jasa Level 1";
        dgMain.Columns["JASA_LEVEL_2"].Caption = "Jasa Level 2";
        dgMain.Columns["JASA_LEVEL_3"].Caption = "Jasa Level 3";
        dgMain.Columns["MONTH_NUM"].Visible = false;

        //((GridViewDataTextColumn)dgMain.Columns["JUMLAH_PENDAPATAN"]).PropertiesTextEdit.DisplayFormatString = "n";

    }
    private static void GenerateChart_JasaJaringanTable(WebChartControl chrMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {
        Cached.CompanyReport_JasaJaringanTable ccTmp = new Cached.CompanyReport_JasaJaringanTable();
        DataTable dt = ccTmp.GetJasaJaringanRekap(iCompanyId, dtmStart, dtmEnd, true);
        DataTable dt2 = ccTmp.GetJasaLevel3Series(iCompanyId, dtmStart, dtmEnd, true);

        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        CompanyProfileData cpd = ccCompany.GetCompanyProfileData(iCompanyId);

        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        chrMain.Titles.Clear();
        ChartTitle _Title = new ChartTitle();
        _Title.Text = String.Format("{0}: {1} to {2}", GetReportName(7), sxStartDate, sxEndDate);
        _Title.Dock = ChartTitleDockStyle.Top;
        _Title.Font = new Font("Arial", 14);
        chrMain.Titles.Add(_Title);

        DateTime dtmTmp = dtmEnd.AddMonths(-11);
        dtmTmp = dtmEnd.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;

        Series[] arrSeries;
        Color[] arrColors;
        DataRow[] dr;
        String[] sxSeries = sxFilteredSeries.Split(new string[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        if (!String.IsNullOrEmpty(sxFilteredSeries))
        {
            List<String> lsSeries = new List<String>();
            for (int i = 0; i < sxSeries.Length; i++)
            {
                lsSeries.Add(sxSeries[i]);
            }
            if (sxSeries.Length < dt2.Rows.Count)
            {
                arrSeries = new Series[sxSeries.Length];
                for (int idx = 0; idx < arrSeries.Length; idx++)
                {
                    arrSeries[idx] = new Series(sxSeries[idx], ViewType.Bar);
                }
            }
            else
            {
                arrSeries = new Series[dt2.Rows.Count];
                for (int idx = 0; idx < arrSeries.Length; idx++)
                {
                    if (lsSeries.Contains(dt2.Rows[idx]["JASA_LEVEL_3"].ToString()))
                    {
                        arrSeries[idx] = new Series(dt2.Rows[idx]["JASA_LEVEL_3"].ToString(), ViewType.Bar);
                    }
                }
            }
            
            
            //else
            //{
            //    arrSeries = new Series[sxSeries.Length];
            //    for (int idx = 0; idx < sxSeries.Length; idx++)
            //    {
            //        if (!String.IsNullOrEmpty(sxSeries[idx]))
            //        {
            //            arrSeries[idx] = new Series(sxSeries[idx], ViewType.Bar);
            //        }
            //    }
            //}
        }
        else
        {
            arrSeries = new Series[dt2.Rows.Count];
            for (int idx = 0; idx < dt2.Rows.Count; idx++)
            {
                arrSeries[idx] = new Series(dt2.Rows[idx]["JASA_LEVEL_3"].ToString(), ViewType.Bar);
            }
        }
        arrColors = new Color[arrSeries.Length];
        for (int i = 0; i < arrSeries.Length; i++)
        {
            arrColors[i] = Color.FromArgb(i, Color.Blue);
        }

        //Series[] arrSeries;
        //Color[] arrColors;
        //DataRow[] dr;

        //arrSeries = new Series[dt2.Rows.Count];
        //for (int idx = 0; idx < dt2.Rows.Count; idx++)
        //{
        //    arrSeries[idx] = new Series(dt2.Rows[idx]["JASA_LEVEL_3"].ToString(), ViewType.Bar);
        //}

        //arrColors = new Color[dt2.Rows.Count];
        //for (int i = 0; i < dt2.Rows.Count; i++)
        //{
        //    arrColors[i] = Color.FromArgb(i, Color.Blue);
        //}

        chrMain.Series.Clear();
		List<String> lsArrSeries = new List<String>();
        for (int i = 0; i < arrSeries.Length; i++)
        {
            lsArrSeries.Add(arrSeries[i].Name.ToString());
        }
        while (dtmTmp.CompareTo(dtmEnd) <= 0)
        {
            dr = dt.Select(
                    String.Format("MONTH_NUM = {0} and YEAR_NUM = {1}", dtmTmp.Month, dtmTmp.Year));
            
            for (int i = 0; i < dr.Length; i++)
            {
                if (i < arrSeries.Length)
                {
                    if (arrSeries[i] != null && dr[i]["JUMLAH_JARINGAN"] != null && lsArrSeries.Contains(dr[i]["JASA_LEVEL_3"].ToString()))//arrSeries[i].Name.Equals(dr[i]["JASA_LEVEL_3"]))
                    {
                        int iValue = Convert.ToInt32(dr[i]["JUMLAH_JARINGAN"]);
                        arrSeries[i].Points.Add(new SeriesPoint(dtmTmp.ToString("MMM/yy"), iValue));
                    }
                }
                else
                {
                    break;
                }
            }
            
            dtmTmp = dtmTmp.AddMonths(1);
        }
        for (int x = 0; x < arrSeries.Length; x++)
        {
            if (arrSeries[x] != null)
            {
                arrSeries[x].PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
                arrSeries[x].LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
                arrSeries[x].Label.Visible = true;
                arrSeries[x].Label.BackColor = System.Drawing.Color.Transparent;
                arrSeries[x].Label.Border.Color = System.Drawing.Color.Transparent;
                chrMain.Series.Add(arrSeries[x]);
            }
        }
        if (chrMain.Diagram != null)
        {
            ((XYDiagram)chrMain.Diagram).AxisX.Label.Staggered = true;
            ((XYDiagram)chrMain.Diagram).AxisY.Title.Text = cpd.sxCompanyName;
            ((XYDiagram)chrMain.Diagram).EnableScrolling = true;
            ((XYDiagram)chrMain.Diagram).EnableZooming = true;
        }
        chrMain.Legend.MaxHorizontalPercentage = 50;
        chrMain.SeriesSorting = SortingMode.Ascending;

        chrMain.DataBind();
    }

    private static void GenerateGrid_PenyebabGangguanTable(ASPxGridView dgMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        Cached.CompanyReport_PenyebabGangguanTable ccTmp = new Cached.CompanyReport_PenyebabGangguanTable();
        DataTable dt = ccTmp.GetPenyebabGangguanDetail(iCompanyId, dtmStart, dtmEnd);
        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        dgMain.Caption = String.Format("{0}: {1} to {2}", GetReportName(8), sxStartDate, sxEndDate);
        dgMain.DataSource = dt;
        dgMain.DataBind();

        dgMain.Settings.ShowHorizontalScrollBar = true;

        //dgMain.Columns["COMPANY_NO"].Caption = "Company No";
        dgMain.Columns["COMPANY_NAME"].Caption = "Nama Perusahaan";
        dgMain.Columns["MONTH_NUM"].Caption = "Bulan";
        dgMain.Columns["YEAR_NUM"].Caption = "Tahun";
        dgMain.Columns["NETWORK_NO"].Caption = "Network No";
        dgMain.Columns["SEBAB"].Caption = "Sebab";
        //dgMain.Columns["TOTAL_SEBAB"].Caption = "Total Sebab";
        dgMain.Columns["TOTAL_SEBAB"].Visible = false;
        dgMain.Columns["COMPANY_NO"].Visible = false;
        dgMain.Columns["COMPANY_ID"].Visible = false;

        //((GridViewDataTextColumn)dgMain.Columns["JUMLAH_PENDAPATAN"]).PropertiesTextEdit.DisplayFormatString = "n";

    }
    private static void GenerateChart_PenyebabGangguanTable(WebChartControl chrMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {
        Cached.CompanyReport_PenyebabGangguanTable ccTmp = new Cached.CompanyReport_PenyebabGangguanTable();
        DataTable dt = ccTmp.GetPenyebabGangguanRekap(iCompanyId, dtmStart, dtmEnd);
        DataTable dt2 = ccTmp.GetPerangkatSeries(iCompanyId, dtmStart, dtmEnd);

        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        CompanyProfileData cpd = ccCompany.GetCompanyProfileData(iCompanyId);

        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        chrMain.Titles.Clear();
        ChartTitle _Title = new ChartTitle();
        _Title.Text = String.Format("{0}: {1} to {2}", GetReportName(8), sxStartDate, sxEndDate);
        _Title.Dock = ChartTitleDockStyle.Top;
        _Title.Font = new Font("Arial", 14);
        chrMain.Titles.Add(_Title);

        DateTime dtmTmp = dtmEnd.AddMonths(-11);
        dtmTmp = dtmEnd.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;


        Series[] arrSeries;
        Color[] arrColors;
        DataRow[] dr;
        String[] sxSeries = sxFilteredSeries.Split(new string[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        if (!String.IsNullOrEmpty(sxFilteredSeries))
        {
            arrSeries = new Series[sxSeries.Length];
            for (int idx = 0; idx < sxSeries.Length; idx++)
            {
                if (!String.IsNullOrEmpty(sxSeries[idx]))
                {
                    arrSeries[idx] = new Series(sxSeries[idx], ViewType.Line);
                }
            }
        }
        else
        {
            arrSeries = new Series[dt2.Rows.Count];
            for (int idx = 0; idx < dt2.Rows.Count; idx++)
            {
                arrSeries[idx] = new Series(dt2.Rows[idx]["SEBAB"].ToString(), ViewType.Bar);
            }
        }
        arrColors = new Color[arrSeries.Length];
        for (int i = 0; i < arrSeries.Length; i++)
        {
            arrColors[i] = Color.FromArgb(i, Color.Blue);
        }

        //Series[] arrSeries;
        //Color[] arrColors;
        //DataRow[] dr;

        //arrSeries = new Series[dt2.Rows.Count];
        //for (int idx = 0; idx < dt2.Rows.Count; idx++)
        //{
        //    arrSeries[idx] = new Series(dt2.Rows[idx]["SEBAB"].ToString(), ViewType.Bar);
        //}

        //arrColors = new Color[dt2.Rows.Count];
        //for (int i = 0; i < dt2.Rows.Count; i++)
        //{
        //    arrColors[i] = Color.FromArgb(i, Color.Blue);
        //}

        chrMain.Series.Clear();
        while (dtmTmp.CompareTo(dtmEnd) <= 0)
        {
            dr = dt.Select(
                    String.Format("MONTH_NUM = {0} and YEAR_NUM = {1}", dtmTmp.Month, dtmTmp.Year));

            for (int i = 0; i < dr.Length; i++)
            {
                if (arrSeries[i] != null && dr[i]["TOTAL_SEBAB"] != null && arrSeries[i].Name.Equals(dr[i]["SEBAB"]))
                {
                    int iValue = Convert.ToInt32(dr[i]["TOTAL_SEBAB"]);
                    arrSeries[i].Points.Add(new SeriesPoint(dtmTmp.ToString("MMM/yy"), iValue));
                }
            }
            dtmTmp = dtmTmp.AddMonths(1);
        }
        for (int x = 0; x < arrSeries.Length; x++)
        {
            if (arrSeries[x] != null)
            {
                arrSeries[x].PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
                arrSeries[x].LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
                arrSeries[x].Label.Visible = true;
                arrSeries[x].Label.BackColor = System.Drawing.Color.Transparent;
                arrSeries[x].Label.Border.Color = System.Drawing.Color.Transparent;
                chrMain.Series.Add(arrSeries[x]);
            }
        }
        if (chrMain.Diagram != null)
        {
            ((XYDiagram)chrMain.Diagram).AxisX.Label.Staggered = true;
            ((XYDiagram)chrMain.Diagram).AxisY.Title.Text = cpd.sxCompanyName;
            ((XYDiagram)chrMain.Diagram).EnableScrolling = true;
            ((XYDiagram)chrMain.Diagram).EnableZooming = true;
        }
        chrMain.Legend.MaxHorizontalPercentage = 50;
        chrMain.SeriesSorting = SortingMode.Ascending;

        chrMain.DataBind();
    }

    private static void GenerateGrid_AktivitasCARSalesTable(ASPxGridView dgMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        Cached.CompanyReport_AktivitasCARSalesTable ccTmp = new Cached.CompanyReport_AktivitasCARSalesTable();
        DataTable dt = ccTmp.GetAktivitasCARSalesDetail(iCompanyId, dtmStart, dtmEnd);
        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        dgMain.Caption = String.Format("{0}: {1} to {2}", GetReportName(9), sxStartDate, sxEndDate);
        dgMain.DataSource = dt;
        dgMain.DataBind();

        dgMain.Settings.ShowHorizontalScrollBar = true;

        dgMain.Columns["TOTAL_COURTESY_CALL_CAR"].Caption = "Total Courtesy Call (CAR)";
        dgMain.Columns["TOTAL_CUSTOMER_VISIT_CAR"].Caption = "Total Customer Visit (CAR)";
        dgMain.Columns["TOTAL_FILE_KONFIGURASI"].Caption = "Total File Konfigurasi";
        dgMain.Columns["TOTAL_FILE_DATEK"].Caption = "Total File Datek";
        dgMain.Columns["TOTAL_FILE_EVALUASI"].Caption = "Total File Evaluasi";
        dgMain.Columns["TOTAL_CUSTOMER_VISIT_SALES"].Caption = "Total Customer Visit (SALES)";
        dgMain.Columns["TOTAL_PROPOSAL_PRESENTASI"].Caption = "Total Proposal Presentasi (SALES)";
        dgMain.Columns["MONTH_NUM"].Caption = "Bulan";
        dgMain.Columns["YEAR_NUM"].Caption = "Tahun";
        dgMain.Columns["CUSTOMER_ID"].Visible = false;

        //((GridViewDataTextColumn)dgMain.Columns["JUMLAH_PENDAPATAN"]).PropertiesTextEdit.DisplayFormatString = "n";

    }
    private static void GenerateChart_AktivitasCARSalesTable(WebChartControl chrMain, int iCompanyId, DateTime dtmStart, DateTime dtmEnd,
        String sxFilteredSeries)
    {
        Cached.CompanyReport_AktivitasCARSalesTable ccTmp = new Cached.CompanyReport_AktivitasCARSalesTable();
        DataTable dt = ccTmp.GetAktivitasCARSalesRekap(iCompanyId, dtmStart, dtmEnd);
        //DataTable dt2 = ccTmp.GetPerangkatSeries(iCompanyId, dtmStart, dtmEnd);

        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        CompanyProfileData cpd = ccCompany.GetCompanyProfileData(iCompanyId);

        String sxStartDate = dtmStart.ToString("dd MMM yyyy");
        String sxEndDate = dtmEnd.ToString("dd MMM yyyy");

        chrMain.Titles.Clear();
        ChartTitle _Title = new ChartTitle();
        _Title.Text = String.Format("{0}: {1} to {2}", GetReportName(9), sxStartDate, sxEndDate);
        _Title.Dock = ChartTitleDockStyle.Top;
        _Title.Font = new Font("Arial", 14);
        chrMain.Titles.Add(_Title);

        //DateTime dtmTmp = dtmEnd.AddMonths(-11);
        //dtmTmp = dtmEnd.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;

        Series[] arrSeries;
        Color[] arrColors;
        String[] sxSeries = sxFilteredSeries.Split(new string[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        if (!String.IsNullOrEmpty(sxFilteredSeries))
        {
            arrSeries = new Series[sxSeries.Length];
            for (int idx = 0; idx < sxSeries.Length; idx++)
            {
                if (!String.IsNullOrEmpty(sxSeries[idx]))
                {
                    arrSeries[idx] = new Series(sxSeries[idx], ViewType.Bar);
                }
            }
        }
        else
        {
            arrSeries = new Series[7];
            arrSeries[0] = new Series("TOTAL COURTESY CALL CAR", ViewType.Bar);
            arrSeries[1] = new Series("TOTAL CUSTOMER VISIT CAR", ViewType.Bar);
            arrSeries[2] = new Series("TOTAL FILE KONFIGURASI", ViewType.Bar);
            arrSeries[3] = new Series("TOTAL FILE DATEK", ViewType.Bar);
            arrSeries[4] = new Series("TOTAL FILE EVALUASI", ViewType.Bar);
            arrSeries[5] = new Series("TOTAL CUSTOMER VISIT SALES", ViewType.Bar);
            arrSeries[6] = new Series("TOTAL PROPOSAL PRESENTASI", ViewType.Bar);
        }
        arrColors = new Color[arrSeries.Length];
        for (int i = 0; i < arrSeries.Length; i++)
        {
            arrColors[i] = Color.FromArgb(i, Color.Blue);
        }

        //Series[] arrSeries;
        //Color[] arrColors;
        ////DataRow[] dr;

        //arrSeries = new Series[7];
        //arrSeries[0] = new Series("Total Courtesy Call (CAR)", ViewType.Bar);
        //arrSeries[1] = new Series("Total Customer Visit (CAR)", ViewType.Bar);
        //arrSeries[2] = new Series("Total File Konfigurasi", ViewType.Bar);
        //arrSeries[3] = new Series("Total File Datek", ViewType.Bar);
        //arrSeries[4] = new Series("Total File Evaluasi", ViewType.Bar);
        //arrSeries[5] = new Series("Total Customer Visit (SALES)", ViewType.Bar);
        //arrSeries[6] = new Series("Total Proposal Presentasi (SALES)", ViewType.Bar);

        //arrColors = new Color[arrSeries.Length];
        //for (int i = 0; i < arrSeries.Length; i++)
        //{
        //    arrColors[i] = Color.FromArgb(i, Color.Blue);
        //}
        int iEnd = dt.Rows.Count;
        int iStart = iEnd <= 12 ? 0 : iEnd - 12;

        DateTime dtmTmp = dtmEnd.AddMonths(-11);

        dtmTmp = dtmTmp.CompareTo(dtmStart) < 0 ? dtmStart : dtmTmp;

        while (dtmTmp.CompareTo(dtmEnd) <= 0)
        {
            DataRow[] dr = dt.Select(
                    String.Format("MONTH_NUM = {0} and YEAR_NUM = {1}", dtmTmp.Month, dtmTmp.Year));

            for (int i = 0; i < arrSeries.Length; i++)
            {
                int iValue = dr.Length == 0 ? 0 : Convert.ToInt32(dr[0][arrSeries[i].Name]);
                arrSeries[i].Points.Add(new SeriesPoint(dtmTmp.ToString("MMM/yy"), iValue));
            }

            dtmTmp = dtmTmp.AddMonths(1);
        }

        chrMain.Series.Clear();
        for (int x = 0; x < arrSeries.Length; x++)
        {
            arrSeries[x].PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].LegendPointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            arrSeries[x].Label.Visible = true;
            arrSeries[x].Label.BackColor = System.Drawing.Color.Transparent;
            arrSeries[x].Label.Border.Color = System.Drawing.Color.Transparent;
            //((LineSeriesView)arrSeries[x].View).Color = arrColors[x];
            //((LineSeriesView)arrSeries[x].View).Transparency = byte.Parse("120");
            chrMain.Series.Add(arrSeries[x]);
        }

        ((XYDiagram)chrMain.Diagram).AxisX.Label.Staggered = true;
        ((XYDiagram)chrMain.Diagram).AxisY.Title.Text = cpd.sxCompanyName;
        chrMain.DataBind();

    }


    public static String GeneratePanel_NetworkJasa()
    {
        String sxOutput = String.Empty;

        DateTime dtmStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        DateTime dtmEnd = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);

        String sxStart = CustomControls.Create_TextBox(KCompanyReport.START_DATE, KControl.INPUT_ID,
            dtmStart.ToString("dd-MMM-yyyy"), true, TextboxType.Date);

        String sxEnd = CustomControls.Create_TextBox(KCompanyReport.END_DATE, KControl.INPUT_ID,
            dtmEnd.ToString("dd-MMM-yyyy"), true, TextboxType.Date);

        //String sxFilterButton = CustomControls.Create_Button(KCompanyReport.FILTER_BUTTON, "FilterReport()", "Filter", 120);

        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Starting Period", sxStart);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Ending Period", sxEnd);
        //sxOutput += CustomControls.BuildFieldSet_PairedLabel("", sxFilterButton);

        return sxOutput;
    }

    public static String GenerateSeriesFilter(int iReportId, int iCompanyId, DateTime dtmStart, DateTime dtmEnd)
    {
        String sxOutput = String.Empty;
        String sxSeries = String.Empty;
        switch (iReportId)
        {
            case 1:
                sxSeries = String.Empty;
                sxSeries += "DEBET" + KGlobal.NEWLINE + "KOREKSI";
                break;
            case 2:
                sxSeries = String.Empty;
                sxSeries += "ONLINE BILLING" + KGlobal.NEWLINE + "ISOLIR";
                sxSeries += KGlobal.NEWLINE + "CABUT" + KGlobal.NEWLINE + "PROSES ISOLIR";
                sxSeries += KGlobal.NEWLINE + "PROSES CABUT";
                break;
            case 3:
                sxSeries = String.Empty;
                sxSeries += "GANGGUAN BERULANG" +
                    KGlobal.NEWLINE + "BUKAN GANGGUAN BERULANG";
                break;
            case 4:
                sxSeries = String.Empty;
                sxSeries += "PASANG BARU" + KGlobal.NEWLINE + "MUTASI";
                sxSeries += KGlobal.NEWLINE + "CABUT" + KGlobal.NEWLINE + "ISOLIR";
                sxSeries += KGlobal.NEWLINE + "BATAL PASANG BARU" + KGlobal.NEWLINE + "BATAL MUTASI";
                sxSeries += KGlobal.NEWLINE + "BUKA ISOLIR";
                break;
            case 5:
                sxSeries = String.Empty;
                sxSeries += "CLOSED - CANCEL" + KGlobal.NEWLINE + "CLOSED - LOST";
                sxSeries += KGlobal.NEWLINE + "CLOSED - WON" + KGlobal.NEWLINE + "INACTIVE";
                sxSeries += KGlobal.NEWLINE + "OPEN";
                break;
            case 6:
                sxSeries = String.Empty;
                sxSeries += "JUMLAH_PENDAPATAN";
                break;
            case 7:
                sxSeries = String.Empty;
                Cached.CompanyReport_JasaJaringanTable ccTmp = new Cached.CompanyReport_JasaJaringanTable();
                DataTable dt = ccTmp.GetJasaLevel3FilterSeries(iCompanyId);//, dtmStart, dtmEnd);
                for (int idx = 0; idx < dt.Rows.Count; idx++)
                {
                    if (idx > 0)
                    {
                        sxSeries += KGlobal.NEWLINE;
                    }
                    sxSeries += dt.Rows[idx]["JASA_LEVEL_3"].ToString();
                }
                break;
            case 8:
                sxSeries = String.Empty;
                Cached.CompanyReport_PenyebabGangguanTable ccPG = new Cached.CompanyReport_PenyebabGangguanTable();
                DataTable dt2 = ccPG.GetPerangkatFilterSeries(iCompanyId);//, dtmStart, dtmEnd);
                for (int idx = 0; idx < dt2.Rows.Count; idx++)
                {
                    if (idx > 0)
                    {
                        sxSeries += KGlobal.NEWLINE;
                    }
                    sxSeries += dt2.Rows[idx]["SEBAB"].ToString();
                }
                break;
            case 9:
                sxSeries = String.Empty;
                sxSeries += "TOTAL COURTESY CALL CAR" + KGlobal.NEWLINE + "TOTAL CUSTOMER VISIT CAR";
                sxSeries += KGlobal.NEWLINE + "TOTAL FILE KONFIGURASI" + KGlobal.NEWLINE + "TOTAL FILE DATEK";
                sxSeries += KGlobal.NEWLINE + "TOTAL FILE EVALUASI" + KGlobal.NEWLINE + "TOTAL CUSTOMER VISIT SALES";
                sxSeries += KGlobal.NEWLINE + "TOTAL PROPOSAL PRESENTASI";
                break;
        }
        String[] sxSplitted = sxSeries.Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);
        foreach (String sxRow in sxSplitted)
        {
            String sxFilter = CustomControls.Create_CheckBox(sxRow, KControl.CHECKBOX_ID, String.Empty, true);
            sxOutput += CustomControls.BuildFieldSet_PairedLabel(sxFilter, sxRow);
        }

        String sxSeriesButton = CustomControls.Create_Button("SERIES_BUTTON", "FilterSeries()", "Filter", 120);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("", sxSeriesButton);
        return sxOutput;
    }
}
